/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.fee.service;

import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.modules.fee.entity.*;
import com.jeeplus.modules.fee.mapper.DataCodeMapper;
import com.jeeplus.modules.fee.mapper.DataValueMapper;
import com.jeeplus.modules.fee.mapper.FeeDataMapper;
import com.jeeplus.modules.sys.entity.Office;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 计费协议数据源Service
 * @author cyf
 * @version 2016-11-21
 */
@Service
@Transactional(readOnly = true)
public class FeeDataService extends CrudService<FeeDataMapper, FeeData> {
	@Autowired
	private DataCodeMapper dataCodeMapper;
	@Autowired
	private DataValueMapper dataValueMapper;


	public FeeData get(String id) {
		return super.get(id);
	}
	
	public List<FeeData> findList(FeeData feeData) {
		return super.findList(feeData);
	}
	
	public Page<FeeData> findPage(Page<FeeData> page, FeeData feeData) {
		return super.findPage(page, feeData);
	}
	
	@Transactional(readOnly = false)
	public void save(FeeData feeData) {
		super.save(feeData);
	}

	//重新导入数据源子表，覆盖
	@Transactional(readOnly = false)
	public void overDetail(FeeData feeData){
		//先实现功能，简化导入过程
		//不管是新增还是修改，都是先删再增 //改成物理删除  by sixp 7.3
		dataCodeMapper.deleteByData(feeData.getId());
		//不管是新增还是修改，都是先删再增 //改成物理删除  by sixp 7.3
		dataValueMapper.deleteByData(feeData.getId());

		for (DataCode feeDataCode : feeData.getFeeDataCodeList()) {
			feeDataCode.setFeeData(feeData);
			feeDataCode.preInsert();
			dataCodeMapper.insert(feeDataCode);

//			//不管是新增还是修改，都是先删再增
//			feeDataValueDao.deleteByDataAndCode(feeData.getId(),feeDataCode.getId());

			for (DataValue feeDataValue : feeDataCode.getFeeDataValueList()) {
				feeDataValue.setFeeData(feeData);
				feeDataValue.setFeeDataCode(feeDataCode);
				feeDataValue.setIdNumber(feeData.getId()+"_"+feeDataValue.getRowNo());
				feeDataValue.preInsert();
				//改成批量保存
//				feeDataValueDao.insert(feeDataValue);
			}
			//批量保存
			dataValueMapper.insertByBatch(feeDataCode.getFeeDataValueList());

		}
	}

	@Transactional(readOnly = false)
	public void delete(FeeData feeData) {
		super.delete(feeData);
	}


	
	public Page<Agreement> findPageByagreement(Page<Agreement> page, Agreement agreement) {
		agreement.setPage(page);
		page.setList(mapper.findListByagreement(agreement));
		return page;
	}
	public Page<Office> findPageByoffice(Page<Office> page, Office office) {
		office.setPage(page);
		page.setList(mapper.findListByoffice(office));
		return page;
	}
	public Page<Consignor> findListByFeeConsignor(Page<Consignor> page, Consignor feeConsignor) {
		feeConsignor.setPage(page);
		page.setList(mapper.findListByFeeConsignor(feeConsignor));
		return page;
	}
	
	//根据数据和明细状态统计计费明细的个数
	public int countDetailByData(String dataId,Integer status){
		return mapper.countDetailByData(dataId,status);
	}

	//删除数据源计算出来的计费明细
	@Transactional(readOnly = false)
	public void deleteDetailsByData(String dataId) {
		mapper.deleteDetailsByData(dataId);
	}

	//根据结算方ID查询数据源信息
	public FeeData findInfoByfeeConsignorID(String feeConsignor){
		return mapper.findInfoByfeeConsignorID(feeConsignor);
	}



}